<?php
/**
 * 成果
 * User: guojing
 * Date: 2020/4/13
 * Time: 16:08
 */

namespace app\api\controller;

use app\api\model\AchievementsType;
use app\common\controller\Api;
use app\api\model\Project as ProjectModel;
use app\api\model\Achievements as AchievementsModel;
use think\Db;
use app\api\model\Category;
use app\api\model\Company as CompanyModel;
use app\api\model\User as UserModel;
use think\Exception;

class Achievements extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    /**
     * 初始化成果添加页
     */
    public function inadd()
    {
        $types = AchievementsType::findListByCondition();
        $res = [
            'types' => $types ? $types : []
        ];
        $this->success('success', $res);
    }

    /**
     * 添加成功
     */
    public function add()
    {
        $userId = UserModel::getCurrUid($this->request->param('user_id', 0));
        if ($userId <= 0) {
            $this->error('请重新登录', [],401);
        }
        $projectId = $this->request->param('project_id', 0);
        $project = [];
        if ($projectId > 0) {
            $project = ProjectModel::findInfoByCondition(['id' => $projectId]);
        }
        if (!$project) {
            $this->error('项目不存在');
        }

        $posts = $this->request->post();

        $publisTime = isset($posts['publish_time']) ? intval(trim($posts['publish_time'])) : 0;
        if ($publisTime <= 0) {
            $this->error('日期为必要信息，不能留空！');
        }

        $typeId = isset($posts['type_id']) ? intval($posts['type_id']) : 0;
        if ($typeId <= 0) {
            $this->error('成果物类别为必要信息，不能留空！');
        }

        $name = isset($posts['name']) ? trim($posts['name']) : '';
        if (!$name) {
            $this->error('成果物名称为必要信息，不能留空！');
        }

        $img = isset($posts['img']) ? $posts['img'] : '';
        $remark = isset($posts['remark']) ? trim($posts['remark']) : '';
        $attachId = isset($posts['attach_id']) ? $posts['attach_id'] : 0;

        $data = [
            'name' => $name,
            'project_id' => $projectId,
            'uid' => $userId,
            'publish_time' => $publisTime,
            'type_id' => $typeId,
            'img' => $img,
            'remark' => $remark,
            'attach_id' => $attachId
        ];
        $achievements = AchievementsModel::addRecord($data);
        if (!$achievements) {
            $this->error('提交失败');
        }
        $this->success('success');
    }

    /**
     * 成果物列表
     */
    public function alist()
    {
        $userId = UserModel::getCurrUid($this->request->param('user_id', 0));
        $page = $this->request->param('page', 1);
        $pageSize = $this->request->param('page_size', 10);
        $projectId = $this->request->param('project_id', 0);

        $project = ProjectModel::findInfoByCondition(['id' => $projectId]);
        if (!$project) {
            $this->success('success', ['list' => [], 'has_more' => 0]);
        }

        $params = [
            'page' => $page,
            'page_size' => $pageSize,
            'project_id' => $projectId
        ];
        if ($userId > 0) {  //已登录
            $params['uid'] = $userId;
        }
        $list = AchievementsModel::dataList($params);
        if ($list) {
            foreach ($list as &$listVal) {
                $listVal['project_name'] = $project['pname'];
            }
        }
        $res = [
            'list' => $list ? $list : [],
            'has_more' => count($list) >= $pageSize ? 1 : 0
        ];
        $this->success('success', $res);
    }

}